import {
  getRankTable
  // getSaleView
  // editSaleRecord
} from '../../api/sale'
import {
  RANK_LIST,
  RANK_CURRENT_ROW,
  RANK_DATA_COUNT,
  RANK_HISTORY_DATA,
  RANK_PAGE_SIZE
} from '../motations-type'

export default {
  state: {
    rankList: [],
    rankDataCount: null,
    rankPageSize: 20,
    rankHistoryData: [],
    row: {
      ctime: '',
      uname: '',
      mprice: '',
      acn: '',
      cnum: '',
      onum: '',
      unum: ''
    }
  },
  getters: {
    rankList: state => state.rankList
  },
  mutations: {
    [RANK_LIST] (state, data) {
      state.rankList = data
    },
    [RANK_CURRENT_ROW] (state, data) {
      state.row = data
    },
    [RANK_DATA_COUNT] (state, data) {
      state.rankDataCount = data
    },
    [RANK_HISTORY_DATA] (state, data) {
      state.rankHistoryData = data
    },
    [RANK_PAGE_SIZE] (state, data) {
      state.rankPageSize = data
    }
  },
  actions: {
    async getRankList ({ commit, rootState }, playload) {
      let data = {}
      let newData = []
      if (Object.keys(playload).length > 0) {
        data = playload
      }
      const resp = await getRankTable(data)
      // console.log(resp)
      resp.data.data.rows.map((item) => {
        newData = [...newData, {
          id: item.id,
          ctime: item.ctime,
          uname: item.uname,
          mprice: item.mprice,
          acn: item.acn,
          cnum: item.cnum,
          onum: item.onum,
          unum: item.acn - item.onum
        }]
      })
      commit(RANK_LIST, newData)
      commit(RANK_DATA_COUNT, resp.data.data.total)
      if (newData.length < rootState.saleRank.rankPageSize) {
        commit(RANK_HISTORY_DATA, newData)
      } else {
        commit(RANK_HISTORY_DATA, newData.slice(0, rootState.saleRank.rankPageSize))
      }
      return resp
    },
    // async editSaleData ({ commit, rootState }, playload) {
    //   let data = {}
    //   if (Object.keys(playload).length > 0) {
    //     data = playload
    //   }
    //   await editSaleRecord(data)
    // },
    async getRankCurrentRow ({ commit, rootState }, row) {
      commit(RANK_CURRENT_ROW, row)
    },
    async changeRankPage ({ commit, rootState }, index) {
      let data = rootState.saleRank.rankList
      let _start = (index - 1) * rootState.saleRank.rankPageSize
      let _end = index * rootState.saleRank.rankPageSize
      commit(RANK_HISTORY_DATA, data.slice(_start, _end))
    },
    async changeRankPageSize ({ commit, rootState }, value) {
      commit(RANK_PAGE_SIZE, value)
    }
  }
}
